package com.revenco.bi.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.json.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.revenco.bi.controller.basecontroller.AbstractUserAwareController;
import com.revenco.bi.model.TnColumnInfo;
import com.revenco.bi.model.TnDataDrillRela;
import com.revenco.bi.service.TnDataDrillRelaService;
import com.revenco.bi.service.TnColumnInfoService;

@Controller
public class DrillController extends AbstractUserAwareController {
	@Autowired
	private TnDataDrillRelaService drillValueService;
	@Autowired
	private TnColumnInfoService tnService;

	@RequestMapping(value = "/drillValue", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String getDrillValue(String columnId,String drillPath) {
		Map<String, List<TnDataDrillRela>> map = new HashMap<String, List<TnDataDrillRela>>();
		map.put("drillList", drillValueService.getDrillValue(columnId,drillPath));
		log.debug(Json.toJson(map));
		return Json.toJson(map);
	}

	/**
	 * 是否显示钻取设置
	 * 
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/drillFlag", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String getDrillFlag(String tableId, String name) {
		TnColumnInfo tnInfo = new TnColumnInfo();
		String name2 = tnService.getNameById(name).getName();
		tnInfo = tnService.getTNColumnInfoByName(tableId, name2);
		Map<String, List<TnDataDrillRela>> map = new HashMap<String, List<TnDataDrillRela>>();
		map.put("drillList",
				drillValueService.getDrillValue(tnInfo.getColumnId(),""));
		log.debug(Json.toJson(map));
		return Json.toJson(map);
	}
	
	
	@RequestMapping(value = "/chartDrillCheck", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String chartDrillCheck(String chartId,String level,String dirllType) {
		String drillPartPara=drillValueService.chartDrillCheck(chartId,level,dirllType);
		return drillPartPara;
	}
	/**
	 * 
	 * @param model
	 * @param response
	 * @param chartId
	 * @param level
	 * @param idValue
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/chartDrillDown", produces = "text/plain;charset=UTF-8")
	@ResponseStatus(HttpStatus.OK)
	public String chartDrillDown(Model model, HttpServletResponse response,
			@RequestParam("chartId") String chartId,
			@RequestParam("columnId") String columnId,
			@RequestParam("namePath") String namePath,
			@RequestParam("conditionId") String conditionId,
			@RequestParam("idValue") String idValue,
			@RequestParam("tableName") String tableName,
			@RequestParam String mulRelationData,
			HttpServletRequest request)
			throws Exception {
		String params = request.getParameter("params");
		if (!StringUtils.isEmpty(params)) {
			params = new String(params.getBytes("ISO8859-1"), "UTF-8");
		}
		String chartXml = drillValueService.chartDrillDown(model, response, chartId,columnId,namePath,conditionId,idValue, tableName, params, mulRelationData);
		model.addAttribute("params", params);
		return chartXml;
	}
}
